<template>
	<div class="mt-4" v-loading="data.mtloading">
		<Editor v-model="data.content" />
		<div class="mt-4">
			<el-button type="primary" @click="onSubmit" v-if="dtasObj.config_id" v-prmis="'configs_update'">{{
				$t('public.ok') }}</el-button>
			<el-button type="primary" @click="onSubmit" v-prmis="'configs_add'" v-else>{{ $t('public.ok') }}</el-button>
			<el-button @click="onCancel">{{ $t('public.cancel') }}</el-button>
		</div>

	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
const { t: $t } = useI18n()
import Editor from "@/components/Editor/index.vue"
import { getconfig } from '@/api/system';
let emit = defineEmits(['infoData'])
let dtasObj = ref({})
let data = reactive({
	mtloading: true,
	content: '',
	config: {
		config_name: 'userAgreement',
		config_content: '',
		config_switch: 1,
		csr_id: 0,
		config_id: 0,
	}
})

// 获取信息
let getagreement = () => {
	data.mtloading = true
	getconfig({ config_name: 'Agreement' }).then(res => {
		if (res.state == 200) {
			data.content = JSON.parse(res.data.config_content)
			for (let key in data.config) {
				data.config[key] = res.data[key]
			}
			data.config.csr_id = res.data.creator
		}
		data.mtloading = false
	})
}

// 保存
let onSubmit = () => {
	let datas = {
		config_name: data.config.config_name,
		config_content: JSON.stringify(data.content),
		config_switch: data.config.config_switch,
		csr_id: data.config.csr_id
	}
	if (data.config.config_id != 0) {
		datas.config_id = data.config.config_id
	}
	dtasObj.value = datas
	emit('infoData', datas)
}

// 取消
let onCancel = () => {
	getagreement()
}

getagreement()

// 暴露方法 方便父组件调用
defineExpose({
	getagreement
})

</script>
